/* 
 * Copyright 2008 Rodrigo Reyes <reyes.rr at gmail dot com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
**/
package net.kornr.canvaschart.client.xy;

import com.google.gwt.widgetideas.graphics.client.Color;

import net.kornr.abstractcanvas.client.ICanvasExt;
import net.kornr.canvaschart.client.core.Annotation;
import net.kornr.canvaschart.client.core.AnnotationSupport;
import net.kornr.canvaschart.client.core.CoordinateSystem;
import net.kornr.canvaschart.client.core.Point;

public class XYLineRenderer implements XYRenderer, AnnotationSupport 
{
	private double lineWidth;
	private String strokeColor = null;
	private String fillColor = null;
	private XYDataSet data = null;
	private boolean enableAnnotation = true;
	private Point lastAnnotation = null;

	public XYLineRenderer(XYDataSet data)
	{
		this(data, 1.0d, "#000000");
	}

	public XYLineRenderer(XYDataSet data, double linewidth, String htmlStrokeColor)
	{
		this(data,linewidth, htmlStrokeColor, null);
	}
	
	public XYLineRenderer(XYDataSet data, double linewidth, String htmlStrokeColor, String htmlFillColor)
	{
		this.data = data;
		this.lineWidth = linewidth;
		strokeColor = htmlStrokeColor;
		fillColor = htmlFillColor;
	}
	
	public void render(ICanvasExt canvas, CoordinateSystem ground) 
	{
		drawPath(ground, canvas, data);	

		if (fillColor != null)
		{
			canvas.setGlobalAlpha(0.5);
			canvas.setFillStyle(new Color(fillColor));
			canvas.fill();
		}

		if (strokeColor != null)
		{
			canvas.setGlobalAlpha(1.0d);
			canvas.setLineWidth(this.lineWidth);
			canvas.setStrokeStyle(new Color(strokeColor));
			canvas.stroke();
		}
	}
	
	protected void drawPath(CoordinateSystem ground, ICanvasExt canvas, XYDataSet data)
	{
		canvas.beginPath();

		Point start = ground.getBottomLeft();
		Point end = ground.getBottomRight();
		canvas.moveTo(start.getX(), start.getY());
		
		canvas.setStrokeStyle(Color.BLACK);
		canvas.setFillStyle(Color.BLUE);
		
		for (int i=0; i<data.size(); i++)
		{
			double x = data.getX(i);
			double y = data.getY(i);

			Point p = ground.getCoord(x,y);
			canvas.lineTo(p.getX(), p.getY());
		}
		canvas.lineTo(end.getX(), end.getY());
		canvas.lineTo(start.getX(), start.getY());
	}

	public double getLineWidth() {
		return lineWidth;
	}

	public void setLineWidth(double lineWidth) {
		this.lineWidth = lineWidth;
	}

	public String getStrokeColor() {
		return strokeColor;
	}

	public void setStrokeColor(String strokeColor) {
		this.strokeColor = strokeColor;
	}

	public String getFillColor() {
		return fillColor;
	}

	public void setFillColor(String fillColor) {
		this.fillColor = fillColor;
	}

	public void setEnableAnnotation(boolean b)
	{
		enableAnnotation = b;
	}
	
	public boolean updateAnnotationFor(CoordinateSystem coordsys, Point dataCoord, Point pixelCoord, Annotation annotation) 
	{
		if (enableAnnotation ==false)
			return false;
		
		if (lastAnnotation == null)
		{
			Point best = this.data.getNearestFromX(dataCoord.getX());
			lastAnnotation = best;
			Point pco = coordsys.getCoord(best);
			annotation.setValue(best.getY());
			annotation.setAlignement(Annotation.CENTER_UP);
			annotation.setPoint(pco);
			annotation.update();
		} else
		{
			Point best = this.data.getNearestFromX(dataCoord.getX());
			if (lastAnnotation.equals(best) == false)
			{
				lastAnnotation = best;
				Point pco = coordsys.getCoord(best);
				annotation.setValue(best.getY());
				annotation.setAlignement(Annotation.CENTER_UP);
				annotation.setPoint(pco);
				annotation.update();				
			}
		}
		
		return false;
	}

}
